package com.site.mapper;

import com.night.common.entity.logistical.Dormitory;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.jdbc.core.SqlProvider;

import java.util.List;

/**
 * @ClassName: DormitoryMapper
 * @author: LJC
 * @date: 2022/10/19-16:21
 * 宿舍表
 */
public interface DormitoryMapper {
    //查询所有信息
    List<Dormitory> getAll();
    //添加宿舍信息
    void addDormitory(Dormitory Dormitory);
    //修改宿舍信息,动态SQL
    @UpdateProvider(type= SqlProvider.class,method = "update")
    void updateDormitory(Dormitory Dormitory);
    @Slf4j
    class SqlProvider {
        //动态更新
        public static String update(final Dormitory dormitory){
            return new SQL(){{
                UPDATE("ss_dormitory");
                //条件
                if (dormitory.getDormitoryType() != null){
                    SET("dormitory_type = #{dormitoryType}");
                }
                if (dormitory.getBuildingNum() != null){
                    SET("building_num = #{buildingNum}");
                }
                if (dormitory.getRoomNum() != null){
                    SET("room_num = #{roomNum}");
                }
                if (dormitory.getBedNum() != null){
                    SET("bed_num = #{bedNum}");
                }
                if (dormitory.getPeopleNum() != null){
                    SET("people_num = #{peopleNum}");
                }
                WHERE("id = #{id}");
            }}.toString();
        }
    }
}
